//
//  XYTableView.m
//  LabHelper
//
//  Created by 找汽配 on 2017/5/31.
//  Copyright © 2017年 XiangYun. All rights reserved.
//

#import "XYScrollTextView.h"

@interface XYScrollTextView ()

@property (nonatomic, weak) UILabel * currentLabel;
@property (nonatomic, weak) UILabel * replaceLabel;

@property (nonatomic, strong) NSTimer * timer;
@property (nonatomic, assign) NSInteger index;

@end

@implementation XYScrollTextView

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.layer.masksToBounds = YES;
    
    UILabel * currentLabel = [[UILabel alloc] init];
    currentLabel.font = self.font;
    currentLabel.textColor = self.textColor;
    currentLabel.textAlignment = self.textAlignment;
    [self addSubview:currentLabel];
    self.currentLabel = currentLabel;
    
    UILabel * replaceLabel = [[UILabel alloc] init];
    replaceLabel.font = self.font;
    replaceLabel.textColor = self.textColor;
    replaceLabel.textAlignment = self.textAlignment;
    [self addSubview:replaceLabel];
    self.replaceLabel = replaceLabel;
}

- (void)dealloc {
    [self stopTimer];
}

#pragma mark - Start

- (void)startFromBottomToTop {
    [self startTimer];
}

- (void)startFromTopToBottom {
    [self startTimer];
}

- (void)startTimer {
    [self stopTimer];
    [self scrollText];
    self.timer = [NSTimer timerWithTimeInterval:2 target:self selector:@selector(scrollText) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}

- (void)stopTimer {
    if (self.timer) {
        [self.timer invalidate];
        self.timer = nil;
    }
}

#pragma mark - Stop

- (void)stop {
    [self stopTimer];
}

#pragma mark - Clear / Create

- (void)reset {
    [self stopTimer];
    self.index = 0;
    if (self.currentLabel) {
        [self.currentLabel removeFromSuperview];
        self.currentLabel = nil;
    }
    if (self.replaceLabel) {
        [self.replaceLabel removeFromSuperview];
        self.replaceLabel = nil;
    }
}

#pragma mark - Scroll

- (void)scrollText {
    if (self.titles.count > 0) {
        self.currentLabel.text = self.titles[[self currentIndex]];
        self.replaceLabel.text = self.titles[[self nextIndex]];
    }
    else {
        self.currentLabel.text = nil;
        self.replaceLabel.text = nil;
    }
    self.currentLabel.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    self.replaceLabel.frame = CGRectMake(0, self.frame.size.height, self.frame.size.width, self.frame.size.height);
    
    if (self.titles.count > 1) {
        [UIView animateWithDuration:1 animations:^{
            self.currentLabel.frame = CGRectMake(0, -self.frame.size.height, self.frame.size.width, self.frame.size.height);
            self.replaceLabel.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
        } completion:^(BOOL finished) {
            UILabel * temp = self.currentLabel;
            self.currentLabel = self.replaceLabel;
            self.replaceLabel = temp;
            self.index = [self nextIndex];
        }];
    }
}

#pragma mark - Index

- (NSInteger)currentIndex {
    return self.index;
}

- (NSInteger)nextIndex {
    return (self.index + 1) % self.titles.count;
}

#pragma mark - Setter

- (void)setFont:(UIFont *)font {
    _font = font;
    self.currentLabel.font = font;
    self.replaceLabel.font = font;
}

- (void)setTextColor:(UIColor *)textColor {
    _textColor = textColor;
    self.currentLabel.textColor = textColor;
    self.replaceLabel.textColor = textColor;
}

- (void)setTextAlignment:(NSTextAlignment)textAlignment {
    _textAlignment = textAlignment;
    self.currentLabel.textAlignment = textAlignment;
    self.replaceLabel.textAlignment = textAlignment;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
